package com.example.newapplication.Fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapBaseIndoorMapInfo;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.example.newapplication.Bean.Address;
import com.example.newapplication.R;
import com.example.newapplication.SealFragment.BackFragment;
import com.example.newapplication.SealFragment.BrowFragment;
import com.example.newapplication.SealFragment.BuyFragment;
import com.example.newapplication.SealFragment.FixFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;


public class LocationFragment extends Fragment {

    private boolean isHaveFloor;
    private ArrayList<Fragment> fragments ;
    private RecyclerView floor;
    ArrayList<String> floors;
    BaiduMap mBaiduMap;
    MapView mapView;
    public MapBaseIndoorMapInfo indoorMapInfo;
    List<Address> list;
    private int selectedPosition = 0 ;
    LocationClient mLocationClient = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_location, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        mBaiduMap.setMyLocationEnabled(true);
        mLocationClient = new LocationClient(getActivity());
//通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(Integer.MAX_VALUE);
//设置locationClientOption
        mLocationClient.setLocOption(option);
        mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING,true,
                null,
                0x550FB8F3,0xAA00FF00));
//注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
        mLocationClient.start();

    }
    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理

    }
    @Override
    public void onPause() {
        mapView.onPause();
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理

    }

    @Override
    public void onDestroy() {
        mapView.onDestroy();
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理

    }
    private void init() {
        floor = getActivity().findViewById(R.id.recyclerView);
        mapView = getActivity().findViewById(R.id.map);


        //地图的初始化
        LatLng centerpos = new LatLng(40.07269, 116.6196); // 首都机场
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(centerpos).zoom(19.5f);
        mBaiduMap = mapView.getMap();
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        floor = getActivity().findViewById(R.id.recyclerView);
        mBaiduMap.setIndoorEnable(true);// 打开室内图

//        Address address1 = new Address();
//        address1.Latitude = 40.072610 ;
//        address1.Longitude = 116.6203 ;
//        list.add(address1);
//        address1 = null;
//        Address address1 = new Address();
//        address1.Latitude = 40.072610 ;
//        address1.Longitude = 116.6203 ;
//        list.add(address1);
//        address1 = null;
//        Address address1 = new Address();
//        address1.Latitude = 40.072610 ;
//        address1.Longitude = 116.6203 ;
//        list.add(address1);
//        address1 = null;
//        Address address1 = new Address();
//        address1.Latitude = 40.072610 ;
//        address1.Longitude = 116.6203 ;
//        list.add(address1);
//        address1 = null;

        mBaiduMap.setOnBaseIndoorMapListener(new BaiduMap.OnBaseIndoorMapListener() {

            @Override
            public void onBaseIndoorMapMode(boolean b, MapBaseIndoorMapInfo mapBaseIndoorMapInfo) {
                if (b) {
                    Log.i("进入室内图","sdfa");
                    indoorMapInfo = mapBaseIndoorMapInfo;
                    // 进入室内图
                    // 通过获取回调参数 mapBaseIndoorMapInfo 来获取室内图信息，包含楼层信息，室内ID等
                    floors = mapBaseIndoorMapInfo.getFloors();

                    if (floors != null && floors.size() > 0) {
                        selectedPosition = floors.size()-1;
                        floor.setAdapter(new mAdapter());
                        floor.setLayoutManager(new LinearLayoutManager(getActivity()));
                    } else {
                        isHaveFloor = false;
                    }
                } else {
                    Log.i("移除室内图","");
                    isHaveFloor = false;
                }
            }
        });
        list = new ArrayList<>();
        if(!list.isEmpty()) list.clear();
        Address address1 = new Address();
        address1.Latitude = 40.072999;
        address1.Longitude = 116.620045;
        list.add(address1);
        address1 = null;

        Address address2 = new Address();
        address2.Latitude = 40.073039;
        address2.Longitude = 116.619551;
        list.add(address2);
        address2 = null;

        Address address3 = new Address();
        address3.Latitude = 40.071693;
        address3.Longitude = 116.619379;
        list.add(address3);
        address3 = null;

        if(list!= null ) {
            for (Address s : list) {
                double latitude = s.getLatitude();
                double longitude = s.getLongitude();
                LatLng point = new LatLng(latitude, longitude);
                Log.i("纬度坐标", String.valueOf(latitude));
                mBaiduMap = mapView.getMap();
                //构建Marker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.location);
                //构建MarkerOption，用于在地图上添加Marker
                OverlayOptions option = new MarkerOptions()
                        .position(point)
                        .icon(bitmap);
                //在地图上添加Marker，并显示
                mBaiduMap.addOverlay(option);
            }
        }
    }

    public void switchFloor(String floor) {
        MapBaseIndoorMapInfo.SwitchFloorError switchFloorError = mBaiduMap.switchBaseIndoorMapFloor(floor, indoorMapInfo.getID()); // 切换楼层信息
        switch (switchFloorError) {
            case SWITCH_OK:
                Toast.makeText(getActivity(),"切换成功到【" + floor + "】",Toast.LENGTH_SHORT).show();
                break;
            case FLOOR_INFO_ERROR:
                Toast.makeText(getActivity(),"室内ID信息错误",Toast.LENGTH_SHORT).show();

                break;
            case FLOOR_OVERLFLOW:
            case FOCUSED_ID_ERROR:
                Toast.makeText(getActivity(),"切换楼层室内ID与当前聚焦室内ID不匹配",Toast.LENGTH_SHORT).show();

                break;

            case SWITCH_ERROR:
                Toast.makeText(getActivity(),"切换楼层错误",Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }


    class mAdapter extends RecyclerView.Adapter<mHolder> {

        @NonNull
        @Override
        public mHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_floors,parent,false);
            mHolder mholder = new mHolder(view);
            return mholder;
        }

        @Override
        public void onBindViewHolder(@NonNull mHolder holder, final int position) {
            holder.text.setText(floors.get(position));
            if (selectedPosition == position) {
                holder.bar.setVisibility(View.VISIBLE);
            } else {
                holder.bar.setVisibility(View.INVISIBLE);
            }
            holder.text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectedPosition = position;
                    switchFloor(floors.get(position));
                    notifyDataSetChanged();
                    notifyItemChanged(selectedPosition);//刷新当前点击item
                    //构建设备坐标
                    list = new ArrayList<>();
                    //构建一楼坐标
                    if(position==1) {
                        mBaiduMap.clear();
                        list.clear();
                        Address address1 = new Address();
                        address1.Latitude = 40.072710;
                        address1.Longitude = 116.62005;
                        list.add(address1);
                        address1 = null;

                        Address address2 = new Address();
                        address2.Latitude = 40.072839;
                        address2.Longitude = 116.6203;
                        list.add(address2);
                        address2 = null;

                        Address address3 = new Address();
                        address3.Latitude = 40.07269;
                        address3.Longitude = 116.61969;
                        list.add(address3);
                        address3 = null;

                        Address address = new Address();
                        address.Latitude = 40.072900;
                        address.Longitude = 116.61955;
                        list.add(address);
                        address = null;
                    }
                    if(position == 2) {
                        list.clear();
                        mBaiduMap.clear();
                        Address address4 = new Address();
                        address4.Latitude = 40.072835;
                        address4.Longitude = 116.6196;
                        list.add(address4);
                        address4 = null;

                        Address address5 = new Address();
                        address5.Latitude = 40.073090;
                        address5.Longitude = 116.6197;
                        list.add(address5);
                        address5 = null;

                        Address address6 = new Address();
                        address6.Latitude = 40.073099;
                        address6.Longitude = 116.6194;
                        list.add(address6);
                        address6 = null;

                        Address address7 = new Address();
                        address7.Latitude = 40.072705;
                        address7.Longitude = 116.6200;
                        list.add(address7);
                        address7 = null;
                    }
                    if(position==3){
                        list.clear();
                        mBaiduMap.clear();
                        if(!list.isEmpty()) list.clear();
                        Address address1 = new Address();
                        address1.Latitude = 40.072999;
                        address1.Longitude = 116.620045;
                        list.add(address1);
                        address1 = null;

                        Address address2 = new Address();
                        address2.Latitude = 40.073039;
                        address2.Longitude = 116.619551;
                        list.add(address2);
                        address2 = null;

                        Address address3 = new Address();
                        address3.Latitude = 40.071693;
                        address3.Longitude = 116.619379;
                        list.add(address3);
                        address3 = null;
                    }
                    //显示坐标
                    if(list!= null ){
                        for(Address s : list) {
                            double latitude = s.getLatitude();
                            double longitude = s.getLongitude();
                            LatLng point = new LatLng(latitude, longitude);
                            Log.i("纬度坐标", String.valueOf(latitude));
                            mBaiduMap = mapView.getMap();
                            //构建Marker图标
                            BitmapDescriptor bitmap = BitmapDescriptorFactory
                                    .fromResource(R.drawable.location);
                            //构建MarkerOption，用于在地图上添加Marker
                            OverlayOptions option = new MarkerOptions()
                                    .position(point)
                                    .icon(bitmap);
                            //在地图上添加Marker，并显示
                            mBaiduMap.addOverlay(option);
                        }
                        list.clear();

                    }else{
                        Toast.makeText(getActivity(),"当前位置没有设备",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return floors.size();
        }
    }
    class mHolder extends RecyclerView.ViewHolder {

        TextView text;
        ConstraintLayout bar;
        public mHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.floor_item);
            bar = itemView.findViewById(R.id.constraintLayout4);
        }
    }
    /**
     * 继承抽象类BDAbstractListener并重写其onReceieveLocation方法来获取定位数据，并将其传给MapView。
     */
    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mapView == null){
                return;
            }
            mBaiduMap = mapView.getMap();
            mBaiduMap.setMyLocationEnabled(true);
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(40f)
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection())
                    .latitude(40.07269)
                    .longitude(116.6196)
                    .build();
            mBaiduMap.setMyLocationData(locData);
            Log.i("当前位置",locData.toString());

//            mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL,true,null,
//                    0xAAFFFF88,0xAA00FF00));

        }
    }
}
